Attribute VB_Name = "Genmod"
Public fpath As String 'Root path of everything
Public logpath As String 'Log file path relitive to root path
Public cmd As New Command

Public Players() As New Player 'Players
Public rooms() As New Room 'The rooms
Public md5 As New clsMD5 'Md5 class

Public Sub Findroot() 'Find the exe root
fpath = App.Path
If Right(fpath, 1) <> "\" Then fpath = fpath & "\"
k = InStr(1, fpath, "src", vbTextCompare)
If k > 0 Then fpath = Mid(fpath, 1, k)
k = 0
Do
lk = k
k = InStr(k + 1, fpath, "\")
Loop While k
fpath = Mid(fpath, 1, lk)
End Sub

Public Function Timestamp(tme As Date) As Double 'Find the timestamp of a date
Dim k As Double
k = CDbl(tme)
Timestamp = Int(k * 10000000000#)
End Function

Public Function Getdate(Timestamp As Double) As Date
Dim k As Double
k = Timestamp / 10000000000#
Getdate = CDate(k)
End Function

Public Function Hasflag(num As Long, flag) As Boolean
Hasflag = ((num And flag) = flag)
End Function

Public Function Setflag(num As Long, flag) As Long
Setflag = num Or flag
End Function

Public Function UnSetflag(num As Long, flag) As Long
UnSetflag = num And (Not flag)
End Function

Public Function Checkban(bnme As String) As Boolean
Dim blist As String
Dim bs() As String
blist = Getprop("Banlist")
bs = Split(blist, vbCrLf) 'Grab each individual ban
For x = 0 To UBound(bs)
If bnme Like bs(x) Then
    Checkban = True
    Exit Function
End If
Next x
Checkban = False
End Function

Public Function RandomStr(ParamArray str()) As String
    Randomize Timer
    tmp = Rnd() * (UBound(str) + 1)
    If tmp < 1 Then tmp = tmp + 1
    RandomStr = str(tmp - 1)
End Function

Public Sub Sendto(sck As Integer, ByVal str As String, Optional ByVal addnewline As Boolean = True)
Debuglog "Sent " & sck & " : " & str
If Form1.Sock(sck).state = 7 Then
    If addnewline Then
        If Right(str, 2) <> vbCrLf Then str = str & vbCrLf
    End If
    Form1.Sock(sck).SendData str
End If
End Sub

Public Sub Loadrooms()
croom = Split(Getprop("Rooms"), vbCrLf)
ReDim rooms(UBound(croom))
For x = 0 To UBound(croom)
rooms(x).location = croom(x)
Next x
End Sub

Public Function Findroomloc(inp As String) As Integer
For x = 0 To UBound(rooms)
If rooms(x).location = inp Then
    Findroomloc = x
    Exit Function
End If
Next x

Findroomloc = -1
End Function

Public Function Findplayerloc(inp As String) As String
Dim out As String
out = "|"
For x = 0 To UBound(Players)
If Players(x).loc = inp Then
    out = out & Trim(str(x)) & "|"
End If
Next x

Findplayerloc = out
End Function
